package com.wonderful.map.view

import android.content.Context
import android.graphics.PointF
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import com.wonderful.image.utils.PointUtil
import com.wonderful.map.R
import com.wonderful.map.bean.PointBean
import com.wonderful.map.constant.MapConstant

/**
 * Desc: 点 布局 -- 放置多个 pointView
 * @Author: wonderful
 * @Time: 2023/5/29 13:34
 */
class PointsView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {

    private val mPointViews: MutableList<PointView> = mutableListOf()

    private val mPointList: MutableList<PointBean> = mutableListOf()

    /**
     * 移除 pointView
     */
    private fun removePointView(pointView: PointView?) {
        pointView?.let {
            mPointList.remove(pointView.getPointBean())
            mPointViews.remove(pointView)
            removeView(pointView)
        }
    }

    /**
     * 根据功能点类型获取 icon 资源
     */
    private fun getPointRes(type: Int): Int {
        return when (type) {
            MapConstant.PointType.INIT -> R.drawable.ic_point_init
            MapConstant.PointType.SUPPLY -> R.drawable.ic_point_charge
            MapConstant.PointType.DROP -> R.drawable.bg_point_blue
            else -> R.drawable.bg_point_green
        }
    }

    /**
     * 添加单个点
     */
    private fun addPoint(
        pointF: PointF,
        point: PointBean,
        @DrawableRes pointRes: Int = R.drawable.bg_point_green,
        isShowArrow: Boolean = false
    ): PointView {
        // 移除相同的点
        var oldPointView = getPointViewById(point.id)
        if (oldPointView == null) {
            oldPointView = getPointViewByName(point.name)
        }
        removePointView(oldPointView)

        val pointView = PointView(context, pointRes = pointRes).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setShowArrow(isShowArrow)
            setPoint(point)
            setPointF(pointF)
        }
        mPointViews.add(pointView)
        addView(pointView)
        return pointView
    }

    /**
     * 添加单个点
     */
    fun addPoint(point: PointBean, vTranslate: PointF, scale: Float, mapHeight: Int): PointView {
        mPointList.add(point)
        val sourcePointF = PointF(point.gridX, point.gridY)
        val viewPointF = PointUtil.sourceToViewCoordinate(sourcePointF, vTranslate, scale)
        val pointRes = getPointRes(point.type)
        return addPoint(viewPointF!!, point, pointRes = pointRes)
    }

    /**
     * 添加多个点
     */
    fun addPoints(points: List<PointBean>?, vTranslate: PointF, scale: Float, mapHeight: Int) {
        points?.let {
            for (point in points) {
                addPoint(point, vTranslate, scale, mapHeight)
            }
        }
    }

    /**
     * 设置多个点
     */
    fun setPoints(points: List<PointBean>?, vTranslate: PointF, scale: Float, mapHeight: Int) {
        mPointViews.clear()
        mPointList.clear()
        removeAllViews()
        addPoints(points, vTranslate, scale, mapHeight)
    }

    /**
     * 刷新全部位置点
     */
    fun refreshView(vTranslate: PointF, scale: Float) {
        for (pointView in mPointViews) {
            pointView.getPointBean()?.let {
                val pointX = PointUtil.sourceToViewX(it.gridX, vTranslate, scale)
                val pointY = PointUtil.sourceToViewY(it.gridY, vTranslate, scale)
                pointView.setPointF(PointF(pointX, pointY))
            }
        }
    }

    /**
     * 根据 pointId 获取 pointView
     */
    fun getPointViewById(pointId: String): PointView? {
        if (pointId.isEmpty()) return null
        for (pointView in mPointViews) {
            if (pointView.getPointId() == pointId) return pointView
        }
        return null
    }

    /**
     * 根据 pointName 获取 pointView
     */
    fun getPointViewByName(pointName: String): PointView? {
        if (pointName.isEmpty()) return null
        for (pointView in mPointViews) {
            if (pointView.getPointName() == pointName) return pointView
        }
        return null
    }

    fun getPointViews(): List<PointView> = mPointViews

    /**
     * 根据 pointId 移除 pointView
     */
    fun removePointViewById(pointId: String) {
        val pointView = getPointViewById(pointId)
        removePointView(pointView)
    }

    /**
     * 移除 pointView
     */
    fun removePointView(point: PointBean) {
        var oldPointView = getPointViewById(point.id)
        if (oldPointView == null) {
            oldPointView = getPointViewByName(point.name)
        }
        removePointView(oldPointView)
    }

    /**
     * 获取临时点
     */
    fun getTempPoint(): PointView? = getPointViewById(MapConstant.TEMP_POINT_ID)

    /**
     * 移除临时点
     */
    fun removeTempPoint() {
        removePointViewById(MapConstant.TEMP_POINT_ID)
    }
}